How to replace link a whole of blog or website pages with javascript code.
Hey everyone, in this page I will share a tutorial and the javascript code. This code use for replacing a specific link in whole of your website/blog with the new link.
For instance, in all of the pages in your blog there are link: https://oldlink.com/abc , https://oldlink.com/def, https://oldlink.com/ijk ....
You want to replace https://oldlink.com/ with https://newlink.com. And if you want it, let us do it.
[post_ads]
Javascript Replace All specific link in Whole of Web/Blog
Javascript as a popular language that used to build a web/blog. May be its too old, but still be the favorites one.
From the example above, it will spend to much time if you editing each of page. Imagine if you have 400 pages. Save your time to editing one by one page!
You can use this simple code. To see the script, share this page in your social media, then visit this page from your shared link.
[lock]
[<script> //<![CDATA[function replaceLinksInWholeWebsite() {var elements = document.getElementsByTagName('a');for (var i = 0; i < elements.length; i++) {var element = elements[i];var href = element.getAttribute('href');// Check if the link starts with "https://www.oldlink.com/"if (href && href.startsWith('https://www.oldlink.com/')) {// Replace the old link with "https://www.newlink.com/"element.setAttribute('href', href.replace('https://www.oldlink.com/', 'https://www.newlink.com/'));}}}// Usage:replaceLinksInWholeWebsite();//]]> </script>]
[/locked]
How to do That?
First, from code above. Replace https://www.oldlink.com/ with the link that you want remove. Then, change https://www.newlink.com, with your new destination link.
[post_ads_2]
[ATID-563 Tsubaki Sannomiya mixdrop.co/f/gnrmedeluvx8jd]
After that, It will be a question "Where I have to place the code?"
You only need to put the code in your HTML template editor. Place the code above </body> tag. You can use this code in all of web/blog that build with javascript. Included blogger (blogspot) or Wordpress.
COMMENTS